/***************************************************************************
 *   Copyright (C) 2008 by Branan Riley   *
 *   branan@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "filelist.h"

#include <QListView>
#include <QStringListModel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>

#include <iostream>

FileList::FileList(QWidget *parent)
 : QWidget(parent)
{
  _model = new QStringListModel(this);

  _view = new QListView(this);
  _view->setModel(_model);
  _view->setEditTriggers(QAbstractItemView::NoEditTriggers);

  _button = new QPushButton("Open");

  _button_layout = new QHBoxLayout;
  _button_layout->addStretch();
  _button_layout->addWidget(_button);

  _layout = new QVBoxLayout;
  _layout->addWidget(_view);
  _layout->addLayout(_button_layout);

  setLayout(_layout);

  connect(_view, SIGNAL(activated(const QModelIndex &)), this, SLOT(indexChanged(const QModelIndex &)));
  connect(_button, SIGNAL(pressed()), this, SLOT(buttonPressed()));
}

FileList::~FileList()
{
}

void FileList::addFiles(QStringList list)
{
  for(QStringList::iterator i = list.begin(); i != list.end(); ++i)
    _files.append(*i);

  _files.sort();
  _model->setStringList(_files);
}

void FileList::indexChanged(const QModelIndex &index)
{
  QString file;
  file = index.data().toString();
  emit selectionChanged(file);
}

void FileList::buttonPressed()
{
  QString file;
  file = _view->currentIndex().data().toString();
  emit selectionChanged(file);
}
